from pymongo import MongoClient

# 源client
client = MongoClient('mongodb://root:yzy%40report@10.11.2.112:27017/')
# 源数据库
origin_db = client["infrared_report"]
# collections = origin_db.list_collection_names(session=None)
# print("原始集合数量：", len(collections))
# print(collections)
#
# # 源document
collection_name = 'victuals'
origin_collection = origin_db[collection_name]
#
target_client = MongoClient('mongodb://root:yzy%40report@10.11.2.111:27001/')
# 目标数据库
target_db = target_client["infrared_report"]
# target_collections = target_db.list_collection_names(session=None)
# print("dev1集合数量：", len(target_collections))
# print(target_collections)
# 目标document
target_collection = target_db[collection_name]
# 如果已经存在，则删除collection
target_collection.drop()

# 原始数据总数
origin_count = origin_collection.count_documents({})
print("原始数据总数:", origin_count)

# 每次跳过多少
skip_count = 0
# 每次查询300条记录
limit = 300
while skip_count < origin_count:
    data = origin_collection.find().skip(skip_count).limit(limit)
    target_collection.insert_many(data)
    skip_count += limit
    print(skip_count)

# 获取已经同步的总记录
result_count = target_collection.count_documents({})

# 如果小于原始数据总数，则同步剩余的数据
if result_count < origin_count:
    data = origin_collection.find().skip(result_count).limit(origin_count - result_count)
    target_collection.insert_many(data)

print("同步总数:", result_count)
